package com.maybach.core.ao.impl;

import com.maybach.common.model.FinanceFlowDO;
import com.maybach.common.model.LoanOrderDO;
import com.maybach.common.query.FinanceFlowQuery;
import com.maybach.core.ao.FinanceFlowAO;
import com.maybach.core.ao.LoanOrderAO;
import com.maybach.core.form.finance.FinanceForm;
import com.maybach.core.manager.FinanceFlowManager;
import com.maybach.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weishuai on 16/3/3.
 */
@Component
public class FinanceFlowAOImpl implements FinanceFlowAO {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    FinanceFlowManager financeFlowManager;
    @Autowired
    LoanOrderAO loanOrderAO;

    @Override
    public List<FinanceFlowDO> queryByBizType(Long outOrderId,String bizType) {
        try {

            FinanceFlowQuery query = new FinanceFlowQuery();
            query.createCriteria().andBizTypeEqualTo(bizType).andLoanOrderIdEqualTo(outOrderId).andStatusGreaterThanOrEqualTo(0);
            return financeFlowManager.selectByQuery(query);
        } catch (Exception e) {
            logger.error("error in queryByBizType,outOrderId: "+outOrderId+",bizType:"+bizType);
            return new ArrayList<>();
        }

    }

    @Override
    public void deleteByOutOrderIdAndType(Long outOrderId,String bizType) {
        try {
            FinanceFlowQuery query=new FinanceFlowQuery();
            query.createCriteria().andLoanOrderIdEqualTo(outOrderId).andBizTypeEqualTo(bizType).andStatusGreaterThanOrEqualTo(0);
            List<FinanceFlowDO> ls=financeFlowManager.selectByQuery(query);
            if(CollectionUtils.isNotEmpty(ls)){
               for(FinanceFlowDO f:ls){
                    f.setStatus(-1);
                   financeFlowManager.updateByPrimaryKeySelective(f);
               }
            }
        } catch (Exception e) {
            logger.error("error in deleteByOutOrderIdAndType,outOrderId: " + outOrderId + ",bizType:"+bizType);
        }
    }

    @Override
    public void addFinanceByBizType(FinanceForm financeForm) {
        try {

            //清空所有
            deleteByOutOrderIdAndType(financeForm.getLoanOrderId(),financeForm.getBizType());
            //批量增加
            List<String> payWays = financeForm.getPayWay();
            List<String>payTimes=financeForm.getPayTime();
            List<String> amounts=financeForm.getAmount();
            List<String> chargeNos=financeForm.getChargeNo();
            List<String> memos=financeForm.getMemo();
            List<String> profImgs=financeForm.getProfImgs();

            if(CollectionUtils.isEmpty(payWays)){
                return;
            }
            for(int i=0;i<payWays.size();i++){
                FinanceFlowDO financeFlowDO=new FinanceFlowDO();
                financeFlowDO.setBizType(financeForm.getBizType());
                financeFlowDO.setLoanOrderId(financeForm.getLoanOrderId());
                String pw=payWays.get(i);
                if(StringUtils.isNotBlank(pw)){
                    financeFlowDO.setPayWay(pw);
                }
                String pt=payTimes.get(i);
                if(StringUtils.isNotBlank(pt)){
                    financeFlowDO.setPayTime(CalendarUtil.toDate(pt, CalendarUtil.TIME_PATTERN));
                }
                String amount=amounts.get(i);
                if(StringUtils.isNotBlank(amount)){
                    financeFlowDO.setAmount(PriceUtil.convertPriceYuanWithComma(amount));
                }
                String chargeNo=chargeNos.get(i);
                if(StringUtils.isNotBlank(chargeNo)){
                    financeFlowDO.setChargeNo(chargeNo);
                }
                try {
                    String memo=memos.get(i);
                    if(StringUtils.isNotBlank(memo)){
                        financeFlowDO.setMemo(memo);
                    }
                } catch (Exception e) {
                    logger.error("error in deleteByOutOrderIdAndType,memos: " + financeForm.toString());
                }
                try {
                    if(CollectionUtils.isNotEmpty(profImgs)&&profImgs.size()>0){
                        String profs = profImgs.get(i);
                        if(StringUtils.isNotBlank(profs)){
                            profs= MayBachStringUtil.replace(profs,";",ImgUrlUtil.SPLIT);
                            financeFlowDO.setProfImgs(profs);
                        }
                    }
                } catch (Exception e) {
                    logger.error("error in deleteByOutOrderIdAndType,profImgs: " + financeForm.toString());
                }

                financeFlowDO.setStatus(0);
                financeFlowManager.insertSelective(financeFlowDO);
            }
        } catch (Exception e) {
            logger.error("error in deleteByOutOrderIdAndType,financeForm: "+financeForm.toString());
        }
    }
}
